home *** CD-ROM | disk | FTP | other *** search
/ MaxiMac 2001 March / MaxiMac 112.iso / Macworld on CD n°112 / Applications (Mac OS Classic) / SiteCam 6.0 démo / WebDocs / classes / SiteCam.class (.txt) next >
Encoding:
Java Class File  |  2000-04-12  |  6.4 KB  |  302 lines  |  [TEXT/MOSS]

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.image.ImageObserver;
  7. import java.net.URL;
  8.  
  9. public class SiteCam extends Applet implements Runnable {
  10.    Image fBrokenImage;
  11.    Thread fThread;
  12.    int fSleep;
  13.    int fImagesToSend;
  14.    int fImagesSent;
  15.    String fErrString;
  16.    boolean fIsSiteCamDoc;
  17.    boolean fDebug;
  18.    boolean fatalErr;
  19.    boolean fGettingFirst = true;
  20.    boolean fSiteProxy = false;
  21.    String fLoadingString;
  22.    URL fURL;
  23.    boolean fAbort = false;
  24.    Image fImage1;
  25.    Image fImage2;
  26.    Image fLastImage;
  27.    boolean fDone = false;
  28.  
  29.    public static void main(String[] args) {
  30.       System.out.println("SiteCam.main");
  31.    }
  32.  
  33.    public void destroy() {
  34.       this.fThread.stop();
  35.    }
  36.  
  37.    public void start() {
  38.       this.fThread.resume();
  39.    }
  40.  
  41.    public void hide() {
  42.       System.out.println("sitecam: hide()");
  43.       if (this.fImagesSent > 0) {
  44.          this.fAbort = true;
  45.       }
  46.  
  47.       System.out.println("sitecam: hide() done");
  48.    }
  49.  
  50.    public void stop() {
  51.       this.fThread.suspend();
  52.    }
  53.  
  54.    public void run() {
  55.       String errStr = "";
  56.  
  57.       try {
  58.          this.GetStreamingImages();
  59.       } catch (Exception e) {
  60.          errStr = "SiteCam: " + ((Throwable)e).toString();
  61.          System.out.println(errStr);
  62.       }
  63.  
  64.    }
  65.  
  66.    public void fatalError(String errString) {
  67.       if (!this.fatalErr) {
  68.          this.fatalErr = true;
  69.  
  70.          try {
  71.             if (this.fErrString == null) {
  72.                this.fErrString = errString;
  73.             } else {
  74.                this.fErrString = this.fErrString + errString;
  75.             }
  76.  
  77.             this.fErrString = this.fErrString + " \n";
  78.          } catch (Exception var2) {
  79.          }
  80.  
  81.          this.fatalErr = false;
  82.       }
  83.  
  84.    }
  85.  
  86.    public void init() {
  87.       this.fErrString = null;
  88.       this.fIsSiteCamDoc = false;
  89.       this.fDebug = false;
  90.       this.fatalErr = false;
  91.       this.fImagesToSend = -1;
  92.       this.fImagesSent = 0;
  93.       String debug = ((Applet)this).getParameter("DEBUG");
  94.       if (debug != null && Integer.parseInt(debug) != 0) {
  95.          this.fDebug = true;
  96.       }
  97.  
  98.       debug = ((Applet)this).getParameter("SITEPROXY");
  99.       if (debug != null && Integer.parseInt(debug) != 0) {
  100.          this.fSiteProxy = true;
  101.       }
  102.  
  103.       String base = null;
  104.       if (((Applet)this).getDocumentBase() != null) {
  105.          base = ((Applet)this).getDocumentBase().toString();
  106.       }
  107.  
  108.       String newBase = base;
  109.       if (base.indexOf(".") != -1) {
  110.          int slash = base.lastIndexOf("/");
  111.          if (slash != -1) {
  112.             newBase = new String(base.getBytes(), 0, slash + 1);
  113.          }
  114.       }
  115.  
  116.       System.out.println("new base " + newBase);
  117.       String fileString = ((Applet)this).getParameter("FILE");
  118.       if (fileString == null) {
  119.          String docString = null;
  120.          docString = ((Applet)this).getParameter("DOC");
  121.          if (docString != null) {
  122.             this.fIsSiteCamDoc = true;
  123.             fileString = newBase + docString + (this.fSiteProxy ? ".sp" : ".mwc") + "?images=1";
  124.          }
  125.       }
  126.  
  127.       this.fLoadingString = ((Applet)this).getParameter("LOADSTRING");
  128.       if (this.fLoadingString == null) {
  129.          this.fLoadingString = "Initializing SiteCam...";
  130.       }
  131.  
  132.       if (fileString == null) {
  133.          this.fErrString = "no DOC or FILE specified.";
  134.       } else {
  135.          System.out.println("fileString " + fileString);
  136.  
  137.          try {
  138.             this.fURL = new URL(fileString);
  139.             System.out.println("SiteCamGrab fURL " + this.fURL.toString());
  140.          } catch (Exception var7) {
  141.             System.out.println(this.fErrString);
  142.             this.fErrString = "SiteCam: Bad URL " + fileString;
  143.             return;
  144.          }
  145.  
  146.          if (((Applet)this).getParameter("images") != null) {
  147.             this.fImagesToSend = Integer.parseInt(((Applet)this).getParameter("images"));
  148.          }
  149.  
  150.          if (((Applet)this).getParameter("delay") != null) {
  151.             this.fSleep = Integer.parseInt(((Applet)this).getParameter("delay"));
  152.          }
  153.  
  154.          if (((Applet)this).getParameter("minutes") != null) {
  155.             int minutes = Integer.parseInt(((Applet)this).getParameter("minutes"));
  156.             this.fSleep += minutes * '\uea60';
  157.          }
  158.  
  159.          if (((Applet)this).getParameter("seconds") != null) {
  160.             int seconds = Integer.parseInt(((Applet)this).getParameter("seconds"));
  161.             this.fSleep += seconds * 1000;
  162.          }
  163.  
  164.          if (this.fDebug) {
  165.             System.out.println("getFile " + this.fURL.getFile().toString());
  166.             if (this.fURL.getRef() != null) {
  167.                System.out.println("getRef " + this.fURL.getRef().toString());
  168.             }
  169.          }
  170.  
  171.          if (fileString == null) {
  172.             this.fatalError("no DOC or FILE specified.");
  173.          } else {
  174.             this.fSleep = -1;
  175.  
  176.             try {
  177.                if (((Applet)this).getParameter("delay") != null) {
  178.                   this.fSleep = Integer.parseInt(((Applet)this).getParameter("delay"));
  179.                }
  180.  
  181.                if (((Applet)this).getParameter("minutes") != null) {
  182.                   int minutes = Integer.parseInt(((Applet)this).getParameter("minutes"));
  183.                   this.fSleep += minutes * '\uea60';
  184.                }
  185.  
  186.                if (((Applet)this).getParameter("seconds") != null) {
  187.                   int seconds = Integer.parseInt(((Applet)this).getParameter("seconds"));
  188.                   this.fSleep += seconds * 1000;
  189.                }
  190.             } catch (Exception var6) {
  191.             }
  192.  
  193.             if (this.fSleep == -1) {
  194.                this.fSleep = this.fIsSiteCamDoc ? 100 : '\uea60';
  195.             }
  196.  
  197.             if (this.fDebug) {
  198.                System.out.println("Starting webcam... Interframe delay " + (double)this.fSleep / (double)1000.0F + " seconds");
  199.             }
  200.  
  201.             this.fThread = new Thread(this);
  202.             this.fThread.suspend();
  203.             this.fThread.start();
  204.          }
  205.       }
  206.    }
  207.  
  208.    public void paint(Graphics g) {
  209.       if (this.fLastImage != null) {
  210.          this.fGettingFirst = false;
  211.          g.drawImage(this.fLastImage, 0, 0, (ImageObserver)null);
  212.       } else {
  213.          g.drawString(((Applet)this).getAppletInfo(), 10, 10);
  214.          if (this.fErrString == null) {
  215.             g.drawString("Loading‚Ķ", 10, 30);
  216.          } else {
  217.             g.drawString(this.fErrString, 10, 30);
  218.          }
  219.       }
  220.  
  221.    }
  222.  
  223.    public void abort() throws Exception {
  224.       this.fAbort = true;
  225.  
  226.       while(!this.fDone) {
  227.          Thread.sleep(100L);
  228.       }
  229.  
  230.    }
  231.  
  232.    public synchronized Image GetNextImage() {
  233.       try {
  234.          Image theImage = ((Applet)this).getImage(this.fURL);
  235.          MediaTracker fMediaTracker = new MediaTracker(this);
  236.          fMediaTracker.addImage(theImage, 1);
  237.          fMediaTracker.waitForAll(120000L);
  238.          if (fMediaTracker.isErrorID(1)) {
  239.             System.out.println("SiteCamGrab fMediaTracker failed " + this.fURL.toString());
  240.             this.fAbort = true;
  241.          }
  242.  
  243.          return theImage;
  244.       } catch (Exception e) {
  245.          System.out.println("SiteCamGrab GetNextImage" + ((Throwable)e).toString() + " URL: " + this.fURL.toString());
  246.          return null;
  247.       }
  248.    }
  249.  
  250.    protected synchronized void GetStreamingImages() throws Exception {
  251.       System.out.println("GetStreamingImages");
  252.  
  253.       while(!this.fAbort) {
  254.          Image img = this.GetNextImage();
  255.          if (img == null) {
  256.             this.fDone = true;
  257.             this.fAbort = true;
  258.             break;
  259.          }
  260.  
  261.          if (this.fImage1 == null) {
  262.             this.fImage1 = img;
  263.             this.TakeImage(this.fImage1);
  264.             if (this.fImage2 != null) {
  265.                this.fImage2.flush();
  266.                this.fImage2 = null;
  267.             }
  268.          } else if (this.fImage2 == null) {
  269.             this.fImage2 = img;
  270.             this.TakeImage(this.fImage2);
  271.             if (this.fImage1 != null) {
  272.                this.fImage1.flush();
  273.                this.fImage1 = null;
  274.             }
  275.          } else {
  276.             System.out.println("SiteCamGrab unexpected state 3.");
  277.             this.fImage1.flush();
  278.             this.fImage1 = img;
  279.             this.TakeImage(this.fImage1);
  280.             this.fImage2.flush();
  281.             this.fImage2 = null;
  282.          }
  283.  
  284.          if (this.fImagesToSend != -1 && this.fImagesSent >= this.fImagesToSend) {
  285.             break;
  286.          }
  287.       }
  288.  
  289.       this.fDone = true;
  290.    }
  291.  
  292.    public synchronized void TakeImage(Image inImage) {
  293.       try {
  294.          this.fLastImage = inImage;
  295.          Graphics g = ((Component)this).getGraphics();
  296.          this.paint(g);
  297.          ++this.fImagesSent;
  298.       } catch (Exception var3) {
  299.       }
  300.    }
  301. }
  302.